package com.lineage.data.item_etcitem.doll;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.DollPowerTable;
import com.lineage.server.datatables.ItemPowerUpdateTable;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Doll;
import com.lineage.server.templates.L1ItemPowerUpdate;
import com.lineage.server.world.World;
import java.util.Map;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DollSolution extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(DollSolution.class);
  
  private static final Random _random = new Random();
  
  public static ItemExecutor get() {
    return new DollSolution();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    try {
      L1ItemPowerUpdate ole1;
      L1ItemInstance ole1item;
      int targObjId = data[0];
      L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
      if (tgItem == null)
        return; 
      L1ItemPowerUpdate info = ItemPowerUpdateTable.get().get(tgItem.getItemId());
      if (info == null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
        return;
      } 
      L1Doll doll = DollPowerTable.get().get_type(tgItem.getItemId());
      if (doll == null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(2477));
        return;
      } 
      if (pc.getDoll(tgItem.getId()) != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        return;
      } 
      if (info.get_mode() == 4) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
        return;
      } 
      if (info.get_nedid() != item.getItemId()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
        return;
      } 
      Map<Integer, L1ItemPowerUpdate> tmplist = ItemPowerUpdateTable.get().get_type_id(tgItem.getItemId());
      if (tmplist.isEmpty()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
        return;
      } 
      int order_id = info.get_order_id();
      L1ItemPowerUpdate tginfo = tmplist.get(Integer.valueOf(order_id + 1));
      if (tginfo == null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
        return;
      } 
      int remainingtime = tgItem.getRemainingTime();
      pc.getInventory().removeItem(item, 1L);
      if (_random.nextInt(1000) < info.get_random()) {
        pc.getInventory().removeItem(tgItem, 1L);
        L1ItemInstance tginfo_item = ItemTable.get().createItem(tginfo.get_itemid());
        if (tginfo_item != null) {
          if (remainingtime > 0)
            tginfo_item.setRemainingTime(remainingtime); 
          tginfo_item.setIdentified(true);
          tginfo_item.setCount(1L);
          pc.getInventory().storeItem(tginfo_item);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("获得崭新的" + tginfo_item.getName() + "。"));
          if (info.getallmsg() != null)
            World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage(
                  String.format(info.getallmsg(), new Object[] { pc.getName(), tginfo_item.getName() }))); 
          return;
        } 
        _log.error("给予物件失败 原因: 指定编号物品不存在(" + tginfo.get_itemid() + ")");
        return;
      } 
      switch (info.get_mode()) {
        case 0:
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(160, tgItem.getLogName(), "$252", "$248"));
          break;
        case 1:
          if (tgItem.getproctect7()) {
            tgItem.setproctect7(false);
            pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
            pc.getInventory().updateItem(tgItem, 4);
            pc.getInventory().saveItem(tgItem, 4);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("该装备受到保护卷的祝福,没发生什么事情"));
            RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), 
                item.getId(), "受到保护卷的保护", pc.getIp());
            return;
          } 
          ole1 = tmplist.get(Integer.valueOf(order_id - 1));
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(String.valueOf(String.valueOf(tgItem.getName())) + "升级失败。"));
          pc.getInventory().removeItem(tgItem, 1L);
          ole1item = ItemTable.get().createItem(ole1.get_itemid());
          ole1item.setIdentified(true);
          ole1item.setCount(1L);
          pc.getInventory().storeItem(ole1item);
          break;
        case 2:
          if (tgItem.getproctect6()) {
            tgItem.setproctect6(false);
            pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
            pc.getInventory().updateItem(tgItem, 4);
            pc.getInventory().saveItem(tgItem, 4);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("该装备受到保护卷的祝福,没发生什么事情"));
            RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), 
                item.getId(), "受到保护卷的保护", pc.getIp());
            return;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(164, tgItem.getLogName(), "$252"));
          pc.getInventory().removeItem(tgItem, 1L);
          break;
        case 3:
          if (_random.nextBoolean()) {
            L1ItemPowerUpdate ole2 = tmplist.get(Integer.valueOf(order_id - 1));
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(String.valueOf(String.valueOf(tgItem.getName())) + "升级失败。"));
            pc.getInventory().removeItem(tgItem, 1L);
            L1ItemInstance ole2item = ItemTable.get().createItem(ole2.get_itemid());
            ole2item.setIdentified(true);
            ole2item.setCount(1L);
            pc.getInventory().storeItem(ole2item);
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(164, tgItem.getLogName(), "$252"));
          pc.getInventory().removeItem(tgItem, 1L);
          break;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
